package com.jingdianjichi.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.application.Dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;

import com.jingdianjichi.subject.domain.entity.SubjectLabelBo;

import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {
@Resource
private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO) {


        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.add.dto,{}", JSON.toJSONString(subjectLabelDTO));
            }
            SubjectLabelBo subjectLabelBo =new SubjectLabelBo();
            BeanUtils.copyProperties(subjectLabelDTO, subjectLabelBo);

            Boolean result = subjectLabelDomainService.add(subjectLabelBo);

            return Result.ok(result);

        } catch (Exception e) {
            log.error("SubjectLabelController.add.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {


        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.update.dto,{}", JSON.toJSONString(subjectLabelDTO));
            }
            SubjectLabelBo subjectLabelBo = new SubjectLabelBo();
            BeanUtils.copyProperties(subjectLabelDTO, subjectLabelBo);

            Boolean result = subjectLabelDomainService.update(subjectLabelBo);

            return Result.ok(result);

        } catch (Exception e) {
            log.error("SubjectLabelController.update.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {


        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.dto,{}", JSON.toJSONString(subjectLabelDTO));
            }
            SubjectLabelBo subjectLabelBo = new SubjectLabelBo();
            BeanUtils.copyProperties(subjectLabelDTO, subjectLabelBo);

            Boolean result = subjectLabelDomainService.delete(subjectLabelBo);

            return Result.ok(result);

        } catch (Exception e) {
            log.error("SubjectCategoryController.update.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("/queryLabelByCategoryId")
    //TODO 查了标签表，作者查了mapping表
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {


        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryLabelByCategoryId.dto,{}", JSON.toJSONString(subjectLabelDTO));
            }
         SubjectLabelBo subjectLabelBo=new SubjectLabelBo();
            BeanUtils.copyProperties(subjectLabelDTO, subjectLabelBo);

            Preconditions.checkNotNull(subjectLabelBo.getCategoryId(),"分类ID不能为空");
            List<SubjectLabelBo> subjectLabelBOList = subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBo);

            List<SubjectLabelDTO> collect = subjectLabelBOList.stream().map(subjectLabelBo1 -> {
                SubjectLabelDTO subjectLabelDTO1 = new SubjectLabelDTO();
                BeanUtils.copyProperties(subjectLabelBo1, subjectLabelDTO1);
                return subjectLabelDTO1;
            }).collect(Collectors.toList());
            return Result.ok(collect);

        } catch (Exception e) {
            log.error("SubjectCategoryController.update.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


}
